var roles = [ {id : '1',text : '管理员'},{id : '2',text : '校长'},{id : '3',text : '老师'},{id : '4',text : '学生'}];
var sexs = [ {id : '3',text : '男'},{id : '4',text : '女'}];
var users = LG.getSelectData(basepath+"user/getUsers.do");
var classes = LG.getSelectData(basepath+"user/getClasses.do");
var is_single = [ {id : '1',text : '是'},{id : '2',text : '否'}];
$(function(){
var gridfilter={};
gridfilter={
		op:'and',
		rules:[{field:'role',value:'4',op:"equal"}]
};
	// 列表结构
var grid = $("#maingrid").ligerGrid(
		{
			columns : [ 		  {display : "班级",name : "class_",width : 120,type : "text",align : "center", 
				render: function(item){
					var html = '';
					for (var i = 0; i < classes.length; i++){
				        if (classes[i]['id'] == item['class_']){
				        	html = classes[i]['text'];
				        	break;
				        }
				    }
					return html;
				}},
			            		{display : "学生姓名",name : "name",width : 100,type : "text",align : "center"},
					            {display : "籍贯",name : "jiguan",width : 150,type : "text",align : "center"},
					            {display : "联系方式",name : "contact",width : 150,type : "text",align : "center"},
					            {display : "高考分数",name : "score",width : 150,type : "text",align : "center"},
					            {display : "政治面貌",name : "polity",width : 150,type : "text",align : "center"},
					            {display : "家庭联系方式",name : "family_contact",width : 150,type : "text",align : "center"},
					            {display : "家庭年收入",name : "income",width : 150,type : "text",align : "center"},
					            {display : "父母亲工作地点",name : "work_address",width : 150,type : "text",align : "center"},
					            {display : "是否单亲",name : "is_single",width : 120,type : "text",align : "center", 
									render: function(item){
										var html = '';
										for (var i = 0; i < is_single.length; i++){
									        if (is_single[i]['id'] == item['is_single']){
									        	html = is_single[i]['text'];
									        	break;
									        }
									    }
										return html;
									}}
							  ],
			dataAction : 'server',
			pageSize : 20,
			sortName : "name",
			delayLoad:true,
			toolbar : {},
			url : basepath + 'SystemWeb/getGrid.do?resource=getStu',
			width : '98%',
			height : '100%',
			heightDiff : -10,
			rownumbers:false,
			checkbox : false
		});
	 grid.loadData(true);
	 $(".l-panel-bbar-inner").hide();
	// 工具条事件
	  function toolbarBtnItemClick(item) {
			switch (item.id) {
			case 'download':
				location.href = basepath + "pages/person/user.xls";
				break;
			case 'input':
				LG.doUpLoadFile({
					url : basepath + "stu/doimport.do"}, grid);
				break;
			case "add":
				showDetail({
						usercode:'',
						username:'',
						password:'',
						phone:'',
						email:'',
						role:'',
						sex:''
				},true,false);
				break;
			
			case "modify" :
				var selected = grid.getSelected();
				if(!selected){
					LG.tip("请选择行");
					return
				}
				showDetail({
					usercode:selected.usercode,
					username:selected.username,
					password:selected.password,
					phone:selected.phone,
					email:selected.email,
					role:selected.role,
					sex:selected.sex
			},false,false);
				break;
			case "delete":
				var selected = grid.getSelected();
				if (!selected) {
					LG.tip('请选择行!');
					return
				}
				
				jQuery.ligerDialog.confirm('确定删除吗?', function(confirm) {
					if (confirm)
						f_delete(selected.usercode);
				});
				break;
			
			}
	  }
	  var detailWin = null, curentData = null, isView=null,isAdd=null;
		function showDetail(data,isAdd,isView) {
			curentData = data;
			var mainform4room ;
			isView = isView;
			isAdd = isAdd;
				// 创建表单结构
				mainform4room = $("#mainform4meeting");
				mainform4room.ligerForm({
					inputWidth: 280,
		    		fields :   [
		    					{display : "帐号",name : "usercode",newline : true,labelWidth : 120,width : 220,space : 20,type : "text",validate : {required : true}	},
		    					{display : "密码",name : "password",newline : true,labelWidth : 120,width : 220,space : 20,type : "text",validate : {required : true}},
		    					{display : "性别",name : "sex",newline : true,labelWidth : 120,width : 220,space : 20,type : "select",comboboxName: "sex_",options: { valueFieldID: "sex", data: sexs},validate : {required : true}},
		    					{display : "角色",name : "role",newline : true,labelWidth : 120,width : 220,space : 20,type : "select",comboboxName: "role_",options: { valueFieldID: "role", data: roles},validate : {required : true}},
		    					{display : "姓名",name : "username",newline : true,labelWidth : 120,width : 220,space : 20,type : "text"},
		    					{display : "电话",name : "phone",newline : true,labelWidth : 120,width : 220,space : 20,type : "text",validate : {isMobile : true}},
		    					{display : "邮箱",name : "email",newline : true,labelWidth : 120,width : 220,space : 20,type : "text",validate : {email : true}}
		    							
		    							],
		    		onAfterSetFields : function(){$(".l-group",mainform4room).width(1050);},
		    		readonly:isView,
					toJSON:JSON2.stringify
		    });
				
				detailWin = $.ligerDialog.open({
					target : $("#detailwindow4meeting"),
					width : 600,
					height : 420,
					title:'人员信息',
					buttons : [ {
						text : '确定',
						onclick : function() {
							if(!isView){
								saveMeeting();
							}else{
								detailWin.hide();
							}
						}
					}, {
						text : '取消',
						onclick : function() {
							detailWin.hide();
						}
					} ]
				});
			if (curentData) {
				LG.loadDataForm(mainform4room,curentData);
			}
			function saveMeeting() {
				jQuery.metadata.setType("attr", "validate"); 
				 LG.validate(mainform4room, { debug: true });
				 if(mainform4room.valid()){
					 var savedata = {};
				    	savedata.usercode = $("#usercode").val();
				    	// 员工信息
				    	savedata.username = $("#username").val();
				    	savedata.password = $("#password").val();
				    	savedata.phone = $("#phone").val();
				    	savedata.email = $("#email").val();
				    	savedata.sex = $("#sex").val();
				    	savedata.role = $("#role").val();
						LG.ajax({
							loading : '正在保存数据中...',
							url: basepath+"user/"+(isAdd ? "insert.do"
									: "update.do"),
							data : savedata,
							success : function(data,Message) {
								LG.tip(Message);
									detailWin.hide();
									f_reload();
								
							},
							error : function(message) {
								LG.tip(message);
							}
						});
				 }
			}
		}
	  
	  
	// 加载toolbar
		var items = [];
		if(globalconfig.role!='4'){
			
		
	items = [
		             { text: '导入模版下载',id:'download',click: toolbarBtnItemClick, img: basepath+"resource/icons/silkicons/application_put.png" },
		             { line:true },
		             { text: '批量导入',id:'input',click: toolbarBtnItemClick, img: basepath+"resource/icons/silkicons/application_get.png" },
		             { line:true }
		            ];}
	grid.toolbarManager.set('items', items);
	function f_reload() {
		grid.loadData();
	}
	 function f_delete(deldata) {
			LG.ajax({
				url:basepath+'user/delete.do',
				loading : '正在删除中...',
				data : {
					usercode : deldata
				},
				success : function(data,message) {
					LG.showSuccess(message);
					grid.loadData();
				},
				error : function(message) {
					LG.showError(message);
				}
			});
		}
	
});




 